<?php
defined('BASEPATH') OR exit('No direct script access allowed');
//部门管理
class Bumen extends CommonController {

	function __construct(){
		
		parent::__construct();
		$this->load->model('User/BumenModel','CurModel');
		
	}
	
	
	//start
	/**
	* @name 获取列表
	* @param page ture char 页面
	* @param size ture char 数量
	* @return 成功返回1
	*/
	//end
	function get_list(){
		$user_id = $this->user_id;
		
		
		$page		= $this->input->post('page');
		$size		= $this->input->post('size');		
		
		if($page<1)$page=1;
		if($size<1)$size=10;
		
		
		$arr = array(
			'name'=>$this->input->post('name',null,''),
			'parent_id'=>$this->input->post('parent_id',null,-1),
			
		);
		
		$res = $this->CurModel->get_list($arr,$page,$size);
		$list = $res['list'];
		$leven_arr = array('一','二','三','四','五','六','七','八','九','十');
		foreach($list as $key=>$val){
			$leven = $this->get_leven($val['parent_id']);
			$list[$key]['leven'] = $leven_arr[$leven].'级';
		}
		
		
		$res['list'] = $list;
		
		$this->data_out($res);
	}
	
	function get_leven($pid){
		if($pid==0){
			return 0;
		}
		$levent = 1;
		while(true){
			$info = $this->CurModel->getRow("id='{$pid}'");
			if($info['parent_id']==0){
				break;
			}
			else{
				$levent++;
				$pid = $info['parent_id'];
			}
		}
		return $levent;
	}
	
	//start
	/**
	* @name 获取全部
	* @return 成功返回1
	*/
	//end
	function get_all(){
		
		$is_filter = $this->input->post('is_filter',null,0);

		//添加缓存开始，ling添加，20191012
		if(file_exists(APPPATH."cache/all_bumen_cache.php")){
			include(APPPATH."cache/all_bumen_cache.php");

			if($bumen_cache){
				$this->data_out($bumen_cache);
			}
		}
		
		$list = $this->get_childs(0,$is_filter);
		if($list){
			foreach($list as $k=>$v){
				$list[$k] = $this->info_out($v);
			}
			
		}
		else{
			$list = array();
		}
		
		$this->data_out($list);
	}

	//start
	/**
	* @name 获取全部
	* @return 成功返回1
	*/
	//end
	function return_all(){
		
		$is_filter = $this->input->post('is_filter',null,0);
		
		$list = $this->get_childs(0,$is_filter);
		if($list){
			foreach($list as $k=>$v){
				$list[$k] = $this->info_out($v);
			}
			
		}
		else{
			$list = array();
		}
		
		return $list;
	}
	
	function get_childs($parent_id=0,$is_filter=0){
		
		$arr = array(
			'parent_id'=>$parent_id
		);
		if(!$is_filter){
			$arr['is_enable'] = 1;
 		}
		
		
		$list = $this->CurModel->getAll($arr,"*",0,0);
		
		if(!$list){
			return false;
		}
		foreach($list as $key=>$val){
			$children = $this->get_childs($val['id'],$is_filter);
			if($children){
				
				$list[$key]['children'] = $children;
			}
		}
		
		return $list;
	}
	
	
	function info_out($info){
		
		return $info;
	}
	
	//start
	/**
	* @name 获取信息
	* @param id ture char ID
	* @return 成功返回1
	*/
	//end
	function get_info(){
		$user_id = $this->user_id;
		
		$result = parent::_get_info();
		if(!$result){
			$this->err_out($this->getError());
		}
		
		$this->data_out($result);
	}
	
	//start
	/**
	* @name 获取信息
	* @param id ture char ID
	* @return 成功返回1
	*/
	//end
	function save(){
		
		$filter = array(
			'name'=>'请输入名称'
		);
		$post = array('name','parent_id','sort','is_enable');
		$farr = array(
			'post'=>$post,
			'filter'=>$filter
		);
		
		$result = parent::_save($farr);
		if(!$result){
			$this->err_out($this->getError());
		}
		
		//写入缓存
		$rel = $this->return_all();
		parent::cache_write('bumen_cache',$rel);
		
		$this->add_log("保存部门【{$result['id']}】信息",'bumen');
		$this->data_out($result);
	}
	
	
	
	//start
	/**
	* @name 删除
	* @param id ture int ID
	* @return 成功返回1
	*/
	//end
	function del(){
		
		$result = parent::_del();
		if(!$result){
			$this->err_out($this->getError());
		}
		
		//写入缓存
		$rel = $this->return_all();
		parent::cache_write('bumen_cache',$rel);
		
		$this->add_log("删除部门【{$result}】",'bumen');
		$this->data_out($result);
		
	}
	
	//start
	/**
	* @name 批量操作
	* @param ids ture int_array ID组合
	* @param act ture int 操作类型enable，disconnected，delete
	* @return 成功返回1
	*/
	//end
	function batch_act(){
		$ids = $this->input->post('ids',null,array());
		$act = $this->input->post('act',null,'');
		
		$act_arr = array('enable','disconnected','delete');
		if(!in_array($act,$act_arr)){
			$this->err_out('错误的错误');
		}
		if(!is_array($ids)){
			$ids = explode(',',$ids);
		}
		if(count($ids)<=0){
			$this->err_out('请选择成员');
		}
		$ls_ids = array();
		foreach($ids as $id){
			if(!empty($id)){
				$ls_ids[] = $id;
			}
		}
		
		if($act=='enable'){
			$this->CurModel->edit(array('is_enable'=>1),"id in(".implode(',',$ls_ids).")");
		}
		elseif($act=='disconnected'){
			$this->CurModel->edit(array('is_enable'=>0),"id in(".implode(',',$ls_ids).")");
		}
		elseif($act=='delete'){
			$this->CurModel->del("id in(".implode(',',$ls_ids).")");
		}

		//写入缓存
		$rel = $this->return_all();
		parent::cache_write('bumen_cache',$rel);

		$this->data_out(1);
	}
	//start
	/**
	* @name 设置是否启用
	* @param id ture int ID
	* @param status ture int 状态
	* @return 成功返回1
	*/
	//end
	function set_enable(){
		$id = $this->input->post('id',null,0);
		$status = $this->input->post('status',null,0);
		$info = $this->CurModel->get_info($id);
		if(!$info){
			$this->err_out('数据不存在');
		}
		
		$this->CurModel->edit(array('is_enable'=>$status),"id ='{$id}'");
		
		$this->data_out(1);
	}
}
